<?php
/**
 * Predicates class file
 *
 * $Rev:: 48            $:  Revision of last commit
 * $Author:: guy        $:  Author of last commit
 * $Date:: 2010-04-19 1#$:  Date of last commit
 * 
 * @package eesc
 */

/**
 * Require rules
 */
require_once BASE_URI . "/lib/Database.class.php";

/**
 * Predicate defines number of courses to be taken
 * from a category.
 *
 * @todo: Extend to allow for weighting of courses through
 * e.g. to solve part III maths where course counts as 2
 * 
 * @package eesc
 * @subpackage classes
 */
class Predicate  {

	// 
	private $id;
	
	// Number of papers from this category
	//private $numberOfCourses;
	
	private $courses = array();
	
	protected $db;
	
	/**
	 * Contructor
	 * 
	 * @param Category $category
	 */
	public function __construct($id = NULL) {
		$this->db = new Database();
		$this->db->connect();
		
		if ($id != NULL)
		{
			$this->id = $id;
			
			$this->courses = $this->getCourses();
		}
	}
	
	/**
	 * Adds a courseID to the link with this predicate
	 * 
	 * @param object $courseID
	 * @return bool
	 */
	public function addCourse($courseID) {
		return $this->db->insert("predicateCourse", array("Predicate" => $this->id,"Course" => $courseID));
	}
	
	/**
	 * Removes all rows with courseID from link with this predicate
	 * 
	 * @param object $courseID
	 * @return bool
	 */
	public function removeCourse($courseID) {
		return $this->db->delete("predicateCourse", array("Predicate" => $this->id,"Course" => $courseID));
	}
	
	/**
	 * @see Database::getTable()
	 */
	public function getTable($format) {
		return $this->db->getTable(get_class($this), $format);
	}
	
	/**
	 * @see Database::add()
	 */
	public function add() {
		return $this->db->add(get_class($this));
	}
	
	/**
	 * Creates the form ready for editing this predicate
	 * 
	 * @param integer $id
	 * @return string $html
	 */
	public function edit($id) {
		$tis = new TIS();
		$courses = $tis->getCourses();
		$chooses = $this->getCourses();
		
		$existing = $this->db->select("predicateCourse","Course","WHERE Predicate = " . $id);
		
		
		$html = "<table><form>";
		foreach ($courses as $course) {
			$selected = "";

			foreach ($existing as $exists)
			{
				if (in_array($course['A_INT_CourseID'], $exists)) {
					$selected = "checked=\"checked\"";
				}
			}
			
			$html .= "<tr><td><input courseid=\"$id\" class=\"course\" id=\"course-{$course['A_INT_CourseID']}\" type=\"checkbox\" value=\"{$course['A_INT_CourseID']}\" $selected/></td>";
			
			$html .= "<td><label for=\"course-{$course['A_INT_CourseID']}\">" . htmlentities($course['STR_SubjectName']) . "</label></td></tr>";
		}
		$html .= "</form></table>\n";
		
		return $this->db->edit(get_class($this), $id) . $html;
		
	}
	
	/**
	 * @see Database::insert()
	 */
	public function insert($assoc_array) {
		return $this->db->insert(get_class($this), $assoc_array);
	}
	
	/**
	 * @see Database::update()
	 */
	public function update($id, $assoc_array) {
		return $this->db->update(get_class($this), array("id" => $id), $assoc_array);
	}
	
	/**
	 * @see Database::delete()
	 */
	public function delete($id) {
		return $this->db->delete(get_class($this), array("id" => $id));
	}	
	
	
	/**
	 * Retrieve a list (category) of courses assigned to this predicate
	 * @return array $courses
	 */
	private function getCourses() {
		$data = $this->db->select("predicateCourse","Course","WHERE Predicate = " . $this->id);
		
		$courses = array();
		
		foreach ($data as $row) {
			$courses[] = $row["Course"];
		} 
		
		return $courses;
	}
	
	/**
	 * Set number of courses for category
	 * 
	 * @param interger $number_of_courses
	 */
	/*
	public function setNumberOfCourses($numberOfCourses) {
		$this->$numberOfCourses = $numberOfCourses;
	}*/
	
	/**
	 * Get number of courses to be choses
	 * 
	 * @return integer $numberOfCourses 
	 */
	public function getNumberOfCourses() {
		$data = $this->db->select("Predicate","numberOfCourses", "WHERE id = " . $this->id );
		return $data[0]['numberOfCourses'];
	}
	
	public function __get($column) {
		$data = $this->db->select("Predicate",$column, "WHERE id = " . $this->id );
		return $data[0][$column];
	}
	
	/**
	 * Check whether choices conform to predicate
	 * 
	 * Counts the number of intersecting courses in
	 * the student choice and the current category
	 * and checks whether the number 
	 *
	 * @param array $courses	List of courses chosen
	 * @return boolean			Conforms to predicate?
	 */
	public function complies(array $choices) {
		if ( count( array_intersect($choices,$this->getCourses() ) ) != $this->numberOfCourses  ) {
			return false;
		}else {
			return true;
		}
	}
	
	/**
	 * ToString method
	 * 
	 * @return string
	 */
	public function __toString() {
		$html = "<h3>{$this->title}</h3>";
		$html .= "<h4>Choose {$this->numberOfCourses} from:</h4>";
			
		$courses = $this->getCourses();
		foreach ($courses as $course) {
			$html .= "<p>";
			$title = $this->db->select("papers","title","WHERE id = " . $course);
			$html .= $title[0]['title'];
			$html .= "</p>";
		}
		
		return $html;
	}
	
}